let core={
    //限流工具类
    throttle:function(method,args,context){
        clearTimeout(method.tId);
        method.tId=setTimeout(function(){
            method.call(context,args);
        },200);
    },
    http:function(option,callback){
     //   console.log(this.cancel)
        this.cancel && this.cancel.abort();
        let opt={load:true},loadHandler,loadTime,
            options ={
            url:"",
            method:"post",
            contentType:"application/x-www-form-urlencoded",
            dataType:"json",
            beforeSend:function(){
                this.load && (loadTime=new Date().getTime()) && (loadHandler = LayUtil.layer.init(function(inner,layer){
                    inner.loading(0,{shade:0.1})
                }))
            },
            success:function(res){
                //处理加载窗口
                if(this.load && loadHandler){
                    let time = 0;
                    if(new Date().getTime() - loadTime < 500){
                        time = 500;
                    }
                    setTimeout(function(){
                        loadHandler.closeLoading();
                    },time)
                }
                switch(res.code){
                    case  CONSTANT.HTTP.SUCCESS:
                        core.prompt.msg(res.msg,{shade: 0.3,time:1200},null);
                        break;
                    case   CONSTANT.HTTP.ERROR:
                        break;
                }
                //如果是一个函数，并且执行这个函数
                (callback instanceof  Function) && callback(res)
            }

        };
        Object.assign(opt,options,option);
        this.cancel=$.ajax(opt);
    },
    //提示相关
    prompt:{
        msg:function (content,option,callback){
            LayUtil.layer.init(function (inner){
                inner.msg(content,option,callback);
            })
        }
    }
};

const  CONSTANT = {
    //http相关
    HTTP:{
        SUCCESS:200,
        ERROR:500
    }



};



// layui工具类
function LayUtil(){}


LayUtil.prototype = {
    construct:LayUtil,
    //弹窗
    layer:(function(LayUtil){
        function Inner(){}

        Inner.prototype={
            construct:Inner,
            init:function(callback){
                let that = this;
                layui.use('layer',function(){
                    that.layer = layui.layer;
                    if(callback instanceof Function){
                        callback(that,that.layer);
                    }
                })
                return this;
            },
            //显示loading加载
            loading:function(config={}){
                this.layer.load(config);
            },
            //关闭加载
            closeLoading:function(){
                this.layer.closeAll('loading');
            },
            //弹窗提示
            msg:function (content,option,callback){
                console.log(layer.msg(content,option,callback));
            }
        }
        LayUtil.layer = new Inner();
    })(LayUtil),
    //form表单
    form:(function (LayUtil){
        function  Inner(){}

        Inner.prototype={
            construct:Inner,
            init:function (callback){
                let that = this;
                layui.use('form',function (){
                    that.form = layui.form;
                    that.form.render();
                    if(callback instanceof Function){
                        callback(that,that.form)
                    }
                });
                return this;
            },
            //提交表单
            submit:function(callback,name,type="submit"){
                this.form.on(type+"("+(name===undefined?'go':name)+")",function (obj){
                    if(callback instanceof  Function){
                        callback(obj);
                        return false;
                    }
                    return true;
                })
            },
            //验证
            verify:function (validator){
                this.form.verify(validator);
            }
        }
         LayUtil.form  = new Inner();
        })(LayUtil)




}
